home *** CD-ROM | disk | FTP | other *** search
/ The Very Best of Atari Inside / The Very Best of Atari Inside 1.iso / mint / mntlib43 / mntlib / dup.c < prev    next >
C/C++ Source or Header  |  1994-02-14  |  1KB  |  69 lines

  1. /* from Dale Schumacher's dLibs library */
  2.  
  3. /* these will have to be adjusted at some time ++jrb */
  4. /* use these with caution, TOS 1.4 still has double re-direction bug! */
  5.  
  6. #include <stddef.h>
  7. #include <fcntl.h>
  8. #include <osbind.h>
  9. #include <errno.h>
  10. #include <mintbind.h>
  11. #include <unistd.h>
  12. #include "lib.h"
  13.  
  14. extern int __mint;
  15.  
  16. int
  17. dup(handle)
  18.     int handle;
  19. {
  20.     register int rv;
  21.     long flags;
  22.  
  23.     if (__mint)
  24.         rv = (int)Fcntl(handle, (long)0, F_DUPFD);
  25.     else
  26.         rv = (int)Fdup(handle);
  27.  
  28.     if (rv < (__SMALLEST_VALID_HANDLE)) {
  29.         errno = -rv;
  30.         rv = -1;
  31.     }
  32.     else
  33.     {
  34.         if (__OPEN_INDEX(rv) < __NHANDLES) {
  35.             __open_stat[__OPEN_INDEX(rv)] =
  36.                 __open_stat[__OPEN_INDEX(handle)];
  37.         }
  38.         if (__mint) {
  39.             flags = (long)Fcntl(rv, (long)0, F_GETFD);
  40.             (void)Fcntl(rv, flags & ~FD_CLOEXEC, F_SETFD);
  41.         }
  42.     }
  43.     return(rv);
  44. }
  45.  
  46. int
  47. dup2(handle1, handle2)
  48.     int handle1, handle2;
  49. {
  50.     int rv;
  51.     long flags;
  52.  
  53.     if (handle1 == handle2)
  54.         return (handle2);
  55.  
  56.     if ((rv = (int)Fforce(handle2, handle1)) < 0)
  57.         errno = -rv;
  58.     else {
  59.         if (__OPEN_INDEX(handle2) < __NHANDLES)
  60.             __open_stat[__OPEN_INDEX(handle2)] =
  61.                 __open_stat[__OPEN_INDEX(handle1)];
  62.         if (__mint) {
  63.             flags = (long)Fcntl(handle2, (long)0, F_GETFD);
  64.             (void)Fcntl(handle2, flags & ~FD_CLOEXEC, F_SETFD);
  65.         }
  66.     }
  67.     return (rv < 0) ? -1 : handle2;
  68. }
  69.